angular.module('share-model', [])

.service 'shareModel', ($log, $rootScope, $ionicPopover, $state,$localStorage) ->
    logger = $log.getInstance 'shareModel'
    logger.info 'init shareModel'
    $scope = $rootScope.$new()
    popover = undefined

    $scope.click = (i) ->
        $scope.items[i].func()
        popover.hide()

    $scope.hideShare = ->
        popover.hide()

    positionView = (target, popoverEle) ->
        # 参考
        # https://github.com/driftyco/ionic/blob/master/js/angular/service/popover.js
        #
        # 因为我们已经使用css来定位，这里不做任何操作
        null

    $ionicPopover.fromTemplateUrl('templates/share-model/share-model.html', {
        scope: $scope
        positionView: positionView # 替换默认的位置处理函数
    }).then (p) -> popover = p

    menuCache = {}
    $rootScope.$on '$stateChangeSuccess', (state) ->
        if _.has menuCache, $state.current.name
            $scope.items = menuCache[$state.current.name]

    # dropdownMenu.set [
    #   {img: '', text: '', func: -> }
    # ]
    set: (items) ->
        menuCache[$state.current.name] = items
        $scope.items = items

    show: (e) ->
        popover.show e


